var express = require("express");
const { default: mongoose } = require("mongoose");
var router = express.Router();
// 导入model.js
var { NotificationModel, JournalismModel, applyModel, noticeClassfyModel } = require("../model/model");

// 通知公告分类获取
router.get('/get_noticeClassFy', async (req, res) => {
  let data = await noticeClassfyModel.aggregate([
    {
      $lookup: {
        from: "notificationfy",
        localField: "_id",
        foreignField: "noticeId",
        as: "children",
        pipeline: [
          {
            $lookup: {
              from: "notificationfy",
              localField: "_id",
              foreignField: "noticeId",
              as: "children",
              pipeline: [
                {
                  $lookup: {
                    from: "notificationfy",
                    localField: "_id",
                    foreignField: "noticeId",
                    as: "children",
                  }
                }
              ]
            }
          }
        ]
      },
    },
    {
      $match: { level: 1 }
    }
  ])
  res.send({
    code: 200,
    data
  })
})

// 通知公告分类添加
router.post('/add_noticeClassFy', async (req, res) => {
  let currentCate = await noticeClassfyModel.findOne({ _id: req.body.ValInfo });
  delete req.body.ValInfo;
  await noticeClassfyModel.create( {...req.body,level:currentCate.level} )
  res.send({
    code: 200,
    msg: '添加分类成功'
  })
})

// 通知公告列表
router.get("/get_notification", async (req, res) => {
  let id = req.query.id;
  console.log(req.query);
  let filter = {};
  if (id !== 'undefined') {
    // filter = {id:new RegExp(id, "i")};
    filter = { noticeClassId: id };
  }
  // let namereg = new RegExp(name, "i");
  list = await NotificationModel.find(filter)
  // .populate("noticeClassId")
  // .skip((pageIndex - 1) * pageSize)
  // .limit(pageSize)
  // .sort()
  // .exec();
  // total = list.length;

  res.send({
    code: 200,
    msg: "展示成功",
    data: list,
  });
});

// 图片新闻列表
router.get("/get_journalism", async (req, res) => {
  // let pageIndex = req.query.pageIndex;
  // let pageSize = req.query.pageSize;
  // let username = req.query.username;
  // let name = req.query.name;
  // console.log(req.query);
  // let list = [];
  // let total = 0;
  // let usernamereg = new RegExp(username, "i");
  // let namereg = new RegExp(name, "i");
  // list = await UserModel.find({
  //   $and: [{ username: usernamereg }, { name: namereg }],
  // })
  //   // .populate("role_id")
  //   .skip((pageIndex - 1) * pageSize)
  //   .limit(pageSize)
  //   .sort({ times: -1 })
  //   .exec();
  // total = list.length;
  let list = await JournalismModel.find()
  let length = await JournalismModel.count()
  // console.log(list);

  res.send({
    code: 200,
    msg: "展示成功",
    data: list,
    length,
  });
});

// 图片新闻列表
router.get("/get_apply", async (req, res) => {
  let list = await applyModel.find()

  res.send({
    code: 200,
    msg: "展示成功",
    data: list,
  });
});

module.exports = router;